/**
 * Used to display on screen notifications
 * 
 * 
 * based on the example in adobe air cookbook
 *
 * @author Marin Todorov
 * @author <a target="_blank" href="http://www.underplot.com">www.underplot.com</a>
 * @version 1.0,  &nbsp; 2008-05-19
 */
/** TAE Comments 
	It's required moo-tool for make nofication pop-up !
	**/
var Notifications = new Class({
	//properties
	title: null,
	message: null,
	
	messageHeight: 100,
	
	width: 300,
	height: 0,
	margin: 20,
	
	messageList: null,
	
	nextMsgId: 1,
	
	//constructor
	initialize: function() {
		this.messageList = new Array();
	},
	
	//methods
	add: function(title, message, icon) {
		this.showWindow();

		if (!this.htmlLoader.loaded) {
			this.add.delay(100, this, new Array(title, message, icon));
			return;
		}
		
		var newMsg = new NotificationMessage(this.nextMsgId++, title, message, icon);
		
		newMsg.setTimeoutId( this.remove.delay(5000, this) );
		
		var doc = this.htmlLoader.window.document;
		var msgDiv = doc.createElement('div');

		msgDiv.className = 'notification';
		msgDiv.id = "msg-"+ (newMsg.id);
		msgDiv.innerHTML = newMsg.template;

		var listDiv = doc.getElementById('list');
		listDiv.appendChild(msgDiv);

		this.messageList.push( newMsg );

		this.adjustWindow();
		
		this.htmlLoader.window.fIn(msgDiv.id);
	},
	
	remove: function() {
		var msg = this.messageList.shift();
		this.removeMessage.delay(105, this, new Array(msg));
	},
	
	removeMessage: function(msg){

		var doc = null;
		try {
			doc = this.htmlLoader.window.document;
			doc.getElementById('list').removeChild(
				doc.getElementById('msg-'+msg.id)
			);
		} catch (e1) {
			// ok swallow
			return;
		}
		
		this.adjustWindow();
		
		if (this.messageList.length==0) 
			this.hideWindow();
		
	},
	
	removeAll: function() {
		while (this.messageList.length>0) {
			var msg = this.messageList.shift();
			$clear(msg.getTimeoutId());
			this.removeMessage( msg );
		}
	},
	
	adjustWindow: function(){
		this.htmlLoader.stage.nativeWindow.height = this.messageList.length * this.messageHeight;	
	},
	
	showWindow: function() {
		if (this.htmlLoader) return;
		
		var visibleBounds = air.Screen.mainScreen.visibleBounds;

		var bounds = new air.Rectangle(
			visibleBounds.right - this.width, //- this.margin, /* left */ 
			this.margin + ((air.Capabilities.os.toLowerCase()=="windows")? 0 : 40), /* top */ 
			this.width, /* width */ 
			this.height /* height */
		);
		var options = new air.NativeWindowInitOptions();
		options.transparent = true;
		
		//transparent windows must have the systemChrome set to none
		options.systemChrome = air.NativeWindowSystemChrome.NONE;
		this.htmlLoader = air.HTMLLoader.createRootWindow( 
			true, //hidden 
			options, 
			false, //no scrollbars
			bounds
		); 
		this.htmlLoader.paintsDefaultBackground = false;
		this.htmlLoader.loadString(
		   "<html>" +
				"<head><script src=\"lib/ui/mootools-beta-1.2b2.js\" type=\"text/javascript\" charset=\"utf-8\"></script>"+
				"<style> .notification {opacity:0;background:url(img/notification.png);width:280px;height:80px; padding:10px; margin-bottom:-20px;} </style>"+
				"<script> function fIn(name) { var e = new Fx.Tween($(name),'opacity', {duration:500});e.set(0);e.start(0.92); } "+
				" function fOut(name) { var e = new Fx.Tween($(name),'opacity', {duration:500});e.set(1);e.start(0); } "+
				"</script>"+
				"</head>" + 
				"<body><div id='list' style='width:300px;'></div></body>"+ 
		   "</html>"
		);
		this.htmlLoader.addEventListener('click',this.onClick.bind(this));
	},
	
	onClick: function(e) {
		this.remove();
	},
	
	hideWindow: function() {
		//this.htmlLoader.stage.nativeWindow.visible = false;
        window.removeEventListener('unload', this.windowCloseEvent);
        if(this.htmlLoader){
			this.htmlLoader.stage.nativeWindow.close();
			this.htmlLoader = null;
		}
	},
	
});

var NotificationMessage = new Class({
	id:null,
	title:null,
	message:null,
	icon:null,
	
	timeoutId:null,
	
	template: 
		"<img src='#icon#' style='margin:10px;float:left;'/>" +
		"<p style='font-weight:bold;'><span id='title'>#title#</span></p>" + 
		"<p><span id='message'>#message#</span></p>" ,
	
	initialize: function(id, title, message, icon) {
		this.title = title;
		this.message = message;
		this.icon = icon;
		this.id = id;

		this.template = this.template.replace(/#icon#/, icon);
		this.template = this.template.replace(/#title#/, title);
		this.template = this.template.replace(/#message#/, message);

	},
	
	setTimeoutId: function (id) {
		this.timeoutId = id;		
	},
	getTimeoutId: function () {
		return this.timeoutId;		
	}

	
});

var Notifications = new Notifications();